import type { Material } from "@/pages/base/material/entity";
import apiClient from "../../apiClient";
import { Result } from "#/api";

export interface PageListReq {
  materialCode?:string;
  materialName?:string;
  status?:number;
  page: number;
  pageSize: number;
}

export interface PageListRes {
  total: number;
  records: Material[];
}

const pageList = (data: PageListReq) => apiClient.post<PageListRes>({ url: "/base/material/pageList", data });
const getInfo = (id: React.Key) => apiClient.get<Material>({ url: `/base/material/${id}` });
const add = (data: Material) => apiClient.post<Material>({ url: "/base/material", data });
const update = (data: Material) => apiClient.put<Material>({ url: "/base/material", data });
const remove = (ids: React.Key[]) => apiClient.delete({ url: "/base/material", params: { ids: ids.join(",") } });

//导出
const exportData = (data: PageListReq) => apiClient.post<any>({
  url: "/base/material/export",
  data
})

//下载上传模板
const downloadTemplate = () => 
	apiClient.get<any>({ 
    url: "/base/material/importTemplate", 
    responseType: "blob"
  });
;

//导入数据
const importData = (file: File) => {
	const formData = new FormData();
	formData.append("file", file);

	return apiClient.post<Result<any>>({
		url: "/base/material/import",
		data: formData,
		headers: {
			// 不要手动设置Content-Type，让浏览器自动处理
			// 浏览器会自动设置正确的Content-Type和boundary
		},
	});
};

export default {
  pageList,
  getInfo,
  add,
  update,
  remove,
  exportData,
  downloadTemplate,
  importData
};
